package librss;
import java.util.Vector;
/*
 * RSSItem.java
 *
 * Created on March 7, 2009, 12:24 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

/**
 *
 * @author alvaro
 */
public class RSSItem {
    
    private String description, link, title, pubDate, author;
    private Vector<String> tags = new Vector<String>();
    
    /** Creates a new instance of RSSItem */
    public RSSItem() {
    }
    
    public Vector<String> getTags(){
        return this.tags;
    }

    public void addTag(String s){
        tags.add(s);
    }
    
    public String getTag(int index){
        return tags.get(index);
    }
    
    public int tagsCounter(){
        return tags.size();
    }
    
    public String getAuthor() {
        return author;
    }

    public String getDescription() {
        return description;
    }

    public String getLink() {
        return link;
    }

    public String getPubDate() {
        return pubDate;
    }

    public String getTitle() {
        return title;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public void setLink(String link) {
        this.link = link;
    }

    public void setPubDate(String pubDate) {
        this.pubDate = pubDate;
    }

    public void setTitle(String title) {
        this.title = title;
    }
    
    public RSSItem(String description, String link, String title, String pubDate, String author){
        this.description = description;
        this.link = link;
        this.title = title;
        this.pubDate = pubDate;
        this.author = author;
    }
    
    public String toString(){
        String result = "";
        result = "pubDate: "+pubDate + "\nTitle: "+title+"\nDescription: "+description+"\nLink: "+link+"\nAuthor: "+author+"\n";
        result = result + "tags: ";
        for(int i = 0;i<tags.size();i++){
            result = result + tags.get(i) + " ";
        }
        result = result.substring(0, result.length()-1);
        return result;
    }
    
    /**
     * 0 equals, 1 this object is bigger, -1 item object is bigger.
     */
    public int compareTo(RSSItem item){
        if(this.pubDate == null)
            return -1;
        if(item.getPubDate() == null)
            return 1;
        
        String pub1 = this.pubDate.substring(pubDate.indexOf(", ")+2, pubDate.indexOf(":")-3);
        String pub2 = item.getPubDate().substring(item.getPubDate().indexOf(", ")+2, item.getPubDate().indexOf(":")-3);
        int year1, year2, month1, month2, day1, day2, hour1 = 0, hour2, minute1, minute2;
        
        year1 = Integer.parseInt(pub1.substring(pub1.lastIndexOf(" ")+1, pub1.length()));
        year2 = Integer.parseInt(pub2.substring(pub2.lastIndexOf(" ")+1, pub2.length()));
        if(year1 > year2)
            return 1;
        if(year2 > year1)
            return -1;
        
        //mismo anyo
        month1 = this.stringMonthToInteger(pub1.substring(pub1.indexOf(" ")+1, pub1.lastIndexOf(" ")));
        month2 = this.stringMonthToInteger(pub2.substring(pub2.indexOf(" ")+1, pub2.lastIndexOf(" ")));
        if(month1 > month2)
            return 1;
        if(month2 > month1)
            return -1;
        //mismo mes
        day1 = Integer.parseInt(pub1.substring(0, pub1.indexOf(" ")));
        day2 = Integer.parseInt(pub2.substring(0, pub2.indexOf(" ")));
        if(day1 > day2)
            return 1;
        if(day2 > day1)
            return -1;
        
        
        //comparar hora
        hour1 = Integer.parseInt(pubDate.substring(pubDate.indexOf(":")-2, pubDate.indexOf(":")));
        hour2 = Integer.parseInt(item.pubDate.substring(item.getPubDate().indexOf(":")-2, item.getPubDate().indexOf(":")));
        if(hour1 > hour2)
            return 1;
        if(hour2 > hour1)
            return -1;
        //comparar minuto
        minute1 = Integer.parseInt(pubDate.substring(pubDate.lastIndexOf(":")-2, pubDate.lastIndexOf(":")));
        minute2 = Integer.parseInt(item.getPubDate().substring(item.getPubDate().lastIndexOf(":")-2, item.getPubDate().lastIndexOf(":")));
        if(minute1 > minute2)
            return 1;
        if(minute2 > minute1)
            return -1;
        
        return 0;
        
    }
    
    private int stringMonthToInteger(String m){
        if(m.equalsIgnoreCase("dec"))
            return 12;
        if(m.equalsIgnoreCase("nov"))
            return 11;
        if(m.equalsIgnoreCase("oct"))
            return 10;
        if(m.equalsIgnoreCase("sep"))
            return 9;
        if(m.equalsIgnoreCase("aug"))
            return 8;
        if(m.equalsIgnoreCase("jul"))
            return 7;
        if(m.equalsIgnoreCase("jun"))
            return 6;
        if(m.equalsIgnoreCase("may"))
            return 5;
        if(m.equalsIgnoreCase("apr"))
            return 4;
        if(m.equalsIgnoreCase("mar"))
            return 3;
        if(m.equalsIgnoreCase("feb"))
            return 2;
        if(m.equalsIgnoreCase("jan"))
            return 1;
        return -1;
    }
    
    /*public static void main(String arg[]) throws Exception {
        RSSItem i1 = new RSSItem(null, null, null, "Tue, 18 Dec 2007 13:25:00 +0000", null);
        RSSItem i2 = new RSSItem(null, null, null, "Tue, 19 jan 2007 13:25:00 +0000", null);
        System.out.println(i1.compareTo(i2));
    }*/
}
